package com.wntms.yjj.web;

import com.github.pagehelper.PageInfo;
import com.wntms.yjj.pojo.Result;
import com.wntms.yjj.pojo.Staff;
import com.wntms.yjj.service.StaffManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/staffs")
public class StaffManagerController {
    @Autowired
    private StaffManagerService staffManagerService;
    @PostMapping
    public Result add(@RequestBody Staff staff){
        System.out.println(staff.getRid());
        boolean flag = staffManagerService.add(staff);
        Result result = null;
        if (flag) {
            result = Result.success(null,"删除成功");
        } else {
            result = Result.error("删除失败");
        }
        return result;
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        System.out.println(id);
        Result result = null;
        boolean flag = staffManagerService.delete(id);
        if (flag) {
            result = Result.success(null,"删除成功");
        } else {
            result = Result.error("删除失败");
        }
        return result;
    }


    @PutMapping("/update")
    public Result update(@RequestBody Staff staff) {
        boolean flag = staffManagerService.update(staff);
        Result result = null;
        if (flag) {
            result = Result.success(null,"修改成功");
        } else {
            result = Result.error("修改失败");
        }
        return result;
    }


    @GetMapping("/findAll")
    public Result findAll() {
        List<Staff> list = staffManagerService.findAll();
        Result result = null;
        if (list != null) {
            result = Result.success(list, "查询成功");
        } else {
            result = Result.error("查询失败");
        }
        return result;
    }


    @RequestMapping("/findByNum")
    public Result findByTel(String num) {
        Staff staff= staffManagerService.findByNum(num);
        Result result = null;
        if (staff != null) {
            result = Result.success(staff, "查询成功");
        } else {
            result = Result.error("查询失败");
        }
        return result;

    }


    @RequestMapping("/findByName")
    public Result findByName(String name) {
        Staff staff= staffManagerService.findByName(name);
        Result result = null;
        if (staff != null) {
            result = Result.success(staff, "查询成功");
        } else {
            result = Result.error("查询失败");
        }
        return result;

    }
    @PostMapping("/findByCondition")
    public Result findByCondition(@RequestBody Staff staff) {
        List<Staff> list=null;
        list=staffManagerService.findByCondition(staff);
        Result result = null;
        if (list != null) {
            result = Result.success(list, "查询成功");
        } else {
            result = Result.error("查询失败");
        }
        return result;
    }

    @GetMapping("/{currentPage}/{pageSize}")
    public Result findByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<Staff> list = staffManagerService.findByPage(currentPage,pageSize);
        Result result = new Result();
        if (list != null){
            PageInfo<Staff> pageInfo = new PageInfo<>(list);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

}
